package com.jingdianjichi.club.wx.handlers;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 86156
 * @version 1.0
 * @date 2024-11-04 17:25
 */
@Component
public class WxChatMsgFactory implements InitializingBean {

    @Resource
    private List<WxChatMessageHandler> wxChatMessageHandlers;

    private final Map<WxChatMsgTypeEnum,WxChatMessageHandler> handlerMap = new HashMap<>();

    public WxChatMessageHandler getHandler(String type){
        WxChatMsgTypeEnum wxChatMsgTypeEnum = WxChatMsgTypeEnum.getByMsgType(type);
        return handlerMap.get(wxChatMsgTypeEnum);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        for (WxChatMessageHandler handler : wxChatMessageHandlers) {
            handlerMap.put(handler.getMsgType(),handler);
        }
    }
}
